package com.clothes.app;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.clothes.mdoel.Worker;
import com.clothes.utils.LogUtil;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.HttpParams;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * Created by llf on 2018/4/1.
 */

public class ClothesApp extends Application {

    //网络监听广播
    //表示是否连接
    public static boolean isConnected;
    public static String netWorkState;
    private static ClothesApp instance;

    @Override
    public void onCreate() {
        super.onCreate();
        initHttp();
        LogUtil.isPrintLog = true;//控制输出日志
    }

    public static ClothesApp getInstance() {
        if(instance == null)
            instance = new ClothesApp();
        return instance;
    }

    private void initHttp(){
        HttpHeaders headers = new HttpHeaders(); //公共消息头
        HttpParams params = new HttpParams();
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.readTimeout(10, TimeUnit.SECONDS);
        builder.writeTimeout(10, TimeUnit.SECONDS);
        builder.connectTimeout(10, TimeUnit.SECONDS);
        OkGo.getInstance().init(this)
                .setOkHttpClient(builder.build())
                .setRetryCount(1)//重连一次
                .addCommonHeaders(headers)
                .addCommonParams(params);
//        netWorkReceiver();
    }

    //每次请求需要的token 与主账号登录获取的不同
    public void setWorkerInfo(Context context, Worker userInfo) {
        SharedPreferences preferences = context.getSharedPreferences("Worker",MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("Worker",new Gson().toJson(userInfo));
        editor.commit();
    }
    public Worker getWorkerInfo(Context context) {
        Worker info = new Worker();
        SharedPreferences preferences = context.getSharedPreferences("Worker",MODE_PRIVATE);
        String json = preferences.getString("Worker","");
        if(!json.equals(""))
            info = new Gson().fromJson(json,Worker.class);
        return info;
    }

    //userName
    public void setUserName(Context context, String token) {
        SharedPreferences preferences = context.getSharedPreferences("userName",MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("userName",token);
        editor.commit();
    }
    public String getUsername(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("userName",MODE_PRIVATE);
        return preferences.getString("userName","");
    }
    //password
    public void setPassWord(Context context, String password) {
        SharedPreferences preferences = context.getSharedPreferences("password",MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("password",password);
        editor.commit();
    }
    public String getPassWord(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("password",MODE_PRIVATE);
        return preferences.getString("password","");
    }

//    private void netWorkReceiver(){
//        IntentFilter filter = new IntentFilter();
//        filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
//        filter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
//        filter.addAction("android.net.wifi.STATE_CHANGE");
//        mNetWorkReceiver = new NetWorkReceiver(netWorkHandler);
//        registerReceiver(mNetWorkReceiver,filter);
//    }


    //网络监听Handler
//    private Handler netWorkHandler = new Handler(){
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            switch (msg.what){
//                case NetWorkState.IS_WIFI:
//                    netWorkState = NetWorkState.WIFI;
//                    isConnected = true;
//                    break;
//                case NetWorkState.IS_MOBILE:
//                    netWorkState = NetWorkState.MOBILE;
//                    isConnected = true;
//                    break;
//                case NetWorkState.NO_CONNECTION:
//                    isConnected = false;
//                    break;
//            }
//        }
//    };

}
